<?php

use Phalcon\Mvc\Model;

class Texts extends Model {

    protected $id;
    protected $author;
    protected $title;
    protected $text_type;
    protected $text;
    protected $visible;
    protected $status;
    protected $recommend;
    protected $create_date;
    protected $update_date;

    public function initialize() {
        $this->belongsTo("author", "Users", "id");
        $this->belongsTo("text_type", "TextTypes", "id");
        $this->hasMany("id", "Comments", "text");
    }

    public function getSource() {
		return "lb_text";
	}


    /**
     * Get the value of Id
     *
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set the value of Id
     *
     * @param mixed id
     *
     * @return self
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get the value of Author
     *
     * @return mixed
     */
    public function getAuthor()
    {
        return $this->author;
    }

    /**
     * Set the value of Author
     *
     * @param mixed author
     *
     * @return self
     */
    public function setAuthor($author)
    {
        $this->author = $author;

        return $this;
    }

    /**
     * Get the value of Title
     *
     * @return mixed
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set the value of Title
     *
     * @param mixed title
     *
     * @return self
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get the value of Text Type
     *
     * @return mixed
     */
    public function getTextType()
    {
        return $this->text_type;
    }

    /**
     * Set the value of Text Type
     *
     * @param mixed text_type
     *
     * @return self
     */
    public function setTextType($text_type)
    {
        $this->text_type = $text_type;

        return $this;
    }

    /**
     * Get the value of Text
     *
     * @return mixed
     */
    public function getText()
    {
        return $this->text;
    }

    /**
     * Set the value of Text
     *
     * @param mixed text
     *
     * @return self
     */
    public function setText($text)
    {
        $this->text = $text;

        return $this;
    }

    /**
     * Get the value of Create Date
     *
     * @return mixed
     */
    public function getCreateDate()
    {
        return $this->create_date;
    }

    /**
     * Set the value of Create Date
     *
     * @param mixed create_date
     *
     * @return self
     */
    public function setCreateDate($create_date)
    {
        $this->create_date = $create_date;

        return $this;
    }

    /**
     * Get the value of Update Date
     *
     * @return mixed
     */
    public function getUpdateDate()
    {
        return $this->update_date;
    }

    /**
     * Set the value of Update Date
     *
     * @param mixed update_date
     *
     * @return self
     */
    public function setUpdateDate($update_date)
    {
        $this->update_date = $update_date;

        return $this;
    }

    public function convertToArray() {

        $comments = $this->comments;
        $list = array();
        foreach($comments as $comment) {
            array_push($list, $comment->convertToArray());
        }

        return array(
            "id"    =>  (int)$this->id,
            "text_type" =>  $this->textTypes->convertToArray(),
            "title" =>  $this->title,
            "author"=>  $this->users->convertToArray(),
            "text"  =>  $this->text,
            "visible" =>  $this->visible,
            "status"  =>  $this->status,
            "comments"  =>  $list,
            "create_date"   =>  $this->create_date,
            "update_date"   =>  $this->update_date
        );
    }

    public function convertToArrayWithUserId($user_id) {

        $comments = $this->comments;
        $list = array();
        foreach($comments as $comment) {

            if($comment->getVisible() == 1) {
                array_push($list, $comment->convertToArray());
            }else {
                if($comment->getFrom() == $user_id) {
                    array_push($list, $comment->convertToArray());
                }else {
                    //do nothing
                }
            }
        }

        return array(
            "id"    =>  (int)$this->id,
            "text_type" =>  $this->textTypes->convertToArray(),
            "title" =>  $this->title,
            "author"=>  $this->users->convertToArray(),
            "text"  =>  $this->text,
            "visible" =>  $this->visible,
            "status"  =>  $this->status,
            "comments"  =>  $list,
            "create_date"   =>  $this->create_date,
            "update_date"   =>  $this->update_date
        );
    }

    /**
     * Get the value of Visible
     *
     * @return mixed
     */
    public function getVisible()
    {
        return $this->visible;
    }

    /**
     * Set the value of Visible
     *
     * @param mixed visible
     *
     * @return self
     */
    public function setVisible($visible)
    {
        $this->visible = $visible;

        return $this;
    }

    /**
     * Get the value of Status
     *
     * @return mixed
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Set the value of Status
     *
     * @param mixed status
     *
     * @return self
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }


    /**
     * Get the value of Recommend
     *
     * @return mixed
     */
    public function getRecommend()
    {
        return $this->recommend;
    }

    /**
     * Set the value of Recommend
     *
     * @param mixed recommend
     *
     * @return self
     */
    public function setRecommend($recommend)
    {
        $this->recommend = $recommend;

        return $this;
    }

}
